package com.longrise.common.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.longrise.common.R;
import com.longrise.common.baseapp.BaseAppLike;


/**
 * Created by callxkj on 2017/2/27.
 * 跟系统手机相关的工具类
 */

public class AppUtil {

    private static Toast mToast;

    /**
     * @return 全局application context
     */
    public static Context getContext() {
        return BaseAppLike.getAPP().getApplicationContext();
    }

    /**
     * 获取本地资源文件
     */
    public static Resources getResources() {
        return getContext().getResources();
    }

    /**
     * 获取配置的String
     *
     * @param resId 资源id
     * @return String
     */
    public static String getString(int resId) {
        return getResources().getString(resId);
    }

    /**
     * 获取配置的color
     *
     * @param resId 资源id
     * @return color
     */
    public static int getColor(int resId) {
        return getResources().getColor(resId);
    }


    /**
     * @return app的版本号
     */
    public static int getAppVersionCode() {
        int versionCode = 0;
        try {
            PackageManager pm = getContext().getPackageManager();
            if (null != pm) {
                PackageInfo packageInfo = pm.getPackageInfo(getContext().getPackageName(), 0);
                if (null != packageInfo) {
                    versionCode = packageInfo.versionCode;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    /**
     * @return app的版本名称
     */
    public static String getAppVersionName() {
        String versionName = "";
        try {
            PackageManager pm = getContext().getPackageManager();
            if (null != pm) {
                PackageInfo packageInfo = pm.getPackageInfo(getContext().getPackageName(), 0);
                if (null != packageInfo) {
                    versionName = packageInfo.versionName;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionName;
    }


    /**
     * 屏幕宽度
     */
    public static int getScreenWidth() {
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 屏幕高度
     */
    public static int getScreenHeight() {
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(float pxValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 根据手机的分辨率from dp 的单位 转成为 px(像素)
     */
    public static int dip2px(float dpValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 将px值转换为sp值，保证文字大小不变
     */
    public static int px2sp(float pxValue) {
        final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    public static int getStatusBar(Context cxt) {
        int statusBarHeight = -1;
        int resourceId = cxt.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }

    /**
     * 判断字符串是否有值，如果为null或者是空字符串或者只有空格或者为"null"字符串，则返回true，否则则返回false
     */
    public static boolean isEmpty(String value) {
        if (value != null && !"".equalsIgnoreCase(value.trim())
                && !"null".equalsIgnoreCase(value.trim())) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 保易健康app的设计图是按iphone6 来的, iphone6的宽度是750px 如果是xhdpi也就是375dp;
     *
     * @param width 设计图上面的宽度 xhdpi的  dp值
     * @return 实际显示的宽度 px
     */
    public static int getRealWidth(int width) {
        return (int) (getScreenWidth() * ((float) width / (float) 375));
    }

    /**
     * @param height 设计图的高度xhdpi的  dp值
     * @return 实际显示的高度 px
     * 因为都是按照屏幕的宽度来适配的 所以高度的比例跟宽度的比例是一样的
     */
    public static int getRealHeight(int height) {
        return (int) (getScreenWidth() * ((float) height / (float) 375));
    }

    /**
     * 启动页全屏 包括状态栏 底部虚拟按键
     */
    public static void setFullScreen(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }


    /**
     * 设置 沉浸式样式  注意root布局要设置 android:fitsSystemWindows="true"
     */
    public static void setTransparentStatusBar(Activity activity) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
            activity.getWindow()
                    .getDecorView()
                    .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
        //android 4.4的不好适配
//       else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            activity.getWindow()
//                    .setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
//                            WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        }
    }

    /**
     * 设置状态栏颜色
     *
     * @param activity 当前需要设置的activity
     * @param color    状态栏颜色
     */
    public static void setStatusBarColor(Activity activity, @ColorInt int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.getWindow().setStatusBarColor(color);
        }
    }

    /**
     * 弹toast的公共类
     */
    public static void showToast(String msg) {
        try {
            View v = View.inflate(getContext(), R.layout.app_toast_view, null);
            TextView tv = (TextView) v.findViewById(R.id.toast_tv);
            tv.setText(msg);
            if (mToast == null) {
                mToast = new Toast(getContext());
            }
            mToast.setView(v);
            mToast.setDuration(Toast.LENGTH_SHORT);
            mToast.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
